package service.review;

import java.util.ArrayList;
import java.util.List;

import util.manager.PageManager;
import util.paramvo.MemberPageListParamVO;
import util.paramvo.UnitPageListParamVO;
import util.returnvo.ReviewListReturnVO;
import util.returnvo.ReviewReturnVO;
import vo.ReviewVO;
import config.PageConfig;
import dao.review.ReviewDAO;
import dao.truth.TruthDAO;

public class ReviewServiceImpl implements ReviewService{

	private ReviewDAO reviewDAO;
	private TruthDAO truthDAO;

	public void setReviewDAO(ReviewDAO reviewDAO) {
		this.reviewDAO = reviewDAO;
	}

	public void setTruthDAO(TruthDAO truthDAO) {
		this.truthDAO = truthDAO;
	}

	@Override
	public List<ReviewReturnVO> getReviews(int majorunit_no, int page) throws Exception {
		List<ReviewReturnVO> rrvList=new ArrayList<ReviewReturnVO>();

		List<ReviewVO> rvList=reviewDAO.getReviews(new UnitPageListParamVO(majorunit_no, PageConfig.REVIEW_ROW_PER_PAGE, page));

		for(ReviewVO rvo:rvList){
			int review_no=rvo.getReview_no();
			rrvList.add(new ReviewReturnVO(rvo, truthDAO.getTruth(review_no), truthDAO.getFalsehood(review_no)));
		}

		return rrvList;
	}

	@Override
	public ReviewListReturnVO getReviewList(int majorunit_no, String page) throws Exception {
		int pageNo=1;

		if(page!=null && !page.equals("")) pageNo=Integer.parseInt(page);

		List<ReviewReturnVO> rrvList=getReviews(majorunit_no, pageNo);

		return new ReviewListReturnVO(rrvList, new PageManager(PageConfig.REVIEW_ROW_PER_PAGE, PageConfig.REVIEW_PAGE_PER_GROUP, reviewDAO.getReviewCount(majorunit_no), pageNo));
	}

	@Override
	public int didWriteReview(ReviewVO param) throws Exception {
		return reviewDAO.didWriteReview(param);
	}

	@Override
	public void registerReview(ReviewVO param) throws Exception {
		reviewDAO.registerReview(param);
	}

	@Override
	public void deleteReview(int review_no) throws Exception {
		reviewDAO.deleteReview(review_no);
	}

	@Override
	public void updateReview(ReviewVO param) throws Exception {
		deleteReview(param.getReview_no());
		reviewDAO.registerReview(param);
	}

	@Override
	public List<ReviewReturnVO> getMyReviews(int member_no, int page) throws Exception {
		List<ReviewReturnVO> rrvList=new ArrayList<ReviewReturnVO>();

		List<ReviewVO> rvList=reviewDAO.getMyReviews(new MemberPageListParamVO(member_no, PageConfig.REVIEW_ROW_PER_PAGE, page));

		for(ReviewVO rvo:rvList){
			int review_no=rvo.getReview_no();
			rrvList.add(new ReviewReturnVO(rvo, truthDAO.getTruth(review_no), truthDAO.getFalsehood(review_no)));
		}

		return rrvList;
	}

	@Override
	public ReviewListReturnVO getMyReviewList(int member_no, String page) throws Exception {
		int pageNo=1;

		if(page!=null && !page.equals("")) pageNo=Integer.parseInt(page);

		List<ReviewReturnVO> rrvList=getMyReviews(member_no, pageNo);

		return new ReviewListReturnVO(rrvList, new PageManager(PageConfig.REVIEW_ROW_PER_PAGE, PageConfig.REVIEW_PAGE_PER_GROUP, reviewDAO.getMyReviewCount(member_no), pageNo));
	}
}
